﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HDT.GUI;
using DTO;
using BLL;

namespace HDT
{
    public partial class FrmLapPN : Form
    {
        NhanVienDTO nhanVien;
        List<ChiTietPhieuNhapDTO> dsCTPhieuNhap;
        private string PRODUCT_IMAGE_FOLDER = "Image\\_Product\\";

        public FrmLapPN()
        {
            InitializeComponent();
            nhanVien = new NhanVienDTO();
        }

        public FrmLapPN(TKDangNhapDTO taiKhoan)
            : this()
        {
            nhanVien = new NhanVienBLL().GetByID(taiKhoan.MaNhanVien);
            List<PhanQuyenDTO> PQs = new PhanQuyenBLL().Select("MaLoaiNV = '" + nhanVien.MaLoaiNhanVien + "'");
            PhanQuyen(PQs.Count == 0 ? new PhanQuyenDTO() : PQs[0]);
        }

        private void PhanQuyen(PhanQuyenDTO phanQuyen)
        {
            btnThemPN.Visible = phanQuyen.LapPhieu;
        }



        private void btnThemPN_Click(object sender, EventArgs e)
        {
            FrmThemPN frm_ThemPN = new FrmThemPN(nhanVien);
            frm_ThemPN.refreshForm = new FrmThemPN.MyDelegate(SearchCTPhieuNhap);
            frm_ThemPN.ShowDialog();
        }

        private void SearchCTPhieuNhap()
        {
            dsCTPhieuNhap = new ChiTietPhieuNhapBLL().Select("MaPhieu LIKE '%" + txtMaPN.Text.Trim()
                + "%' AND MaMatHang LIKE '%" + txtMaMH.Text.Trim() + "%'");
            LoadCTPhieuNhapData(dsCTPhieuNhap);
        }

        private void LoadCTPhieuNhapData(List<ChiTietPhieuNhapDTO> dsChiTiet)
        {
            dgvCTPN.Rows.Clear();
            foreach (ChiTietPhieuNhapDTO chiTiet in dsChiTiet)
            {
                DataGridViewRow row = (DataGridViewRow)dgvCTPN.Rows[0].Clone();
                row.Cells[0].Value = chiTiet.MaChiTiet;
                row.Cells[1].Value = chiTiet.MaPhieuNhap;
                row.Cells[2].Value = new PhieuNhapBLL().GetByID(chiTiet.MaPhieuNhap).ThoiGian.ToString("dd/MM/yyyy");
                row.Cells[3].Value = new MatHangBLL().GetByID(chiTiet.MaMatHang).TenMatHang;
                row.Cells[4].Value = chiTiet.HanSuDung == null ? "N/A" : chiTiet.HanSuDung.Value.ToString("dd/MM/yyyy");
                row.Cells[5].Value = chiTiet.SoLuongNhap;
                row.Cells[6].Value = chiTiet.DonGiaNhap.ToString("#,##0.00");
                dgvCTPN.Rows.Add(row);
            }
        }

        private void LoadProductImage(string filename)
        {
            try
            {
                string path = PRODUCT_IMAGE_FOLDER + filename;
                Image img = Image.FromFile(path);
                picMH.Image = img;
            }
            catch
            {
                picMH.Image = null;
            }
        }

        private void LoadDetail(ChiTietPhieuNhapDTO chiTiet)
        {
            try
            {
                PhieuNhapDTO phieuNhap = new PhieuNhapBLL().GetByID(chiTiet.MaPhieuNhap);
                txtMaPhieu.Text = phieuNhap.MaPhieu;
                txtNgayNhap.Text = phieuNhap.ThoiGian.ToString("dd/MM/yyyy");
                txtNhaCC.Text = phieuNhap.NhaCungCap;
                txtMaNV.Text = phieuNhap.MaNhanVien;
            }
            catch
            {
                txtMaPhieu.Clear();
                txtNgayNhap.Clear();
                txtNhaCC.Clear();
                txtMaNV.Clear();
            }

            try
            {
                MatHangDTO matHang = new MatHangBLL().GetByID(chiTiet.MaMatHang);
                txtTenMH.Text = matHang.TenMatHang;
                try
                {
                    txtLoaiMH.Text = new LoaiMatHangBLL().GetByID(matHang.MaLoaiMatHang).TenLoaiMatHang;
                }
                catch
                {
                    txtLoaiMH.Text = "N/A";
                }
                txtDVT.Text = matHang.DonViTinh;
                txtTrangThai.Text = matHang.TrangThai ? "Đang kinh doanh" : "Không kinh doanh";
                try
                {
                    LoadProductImage(matHang.HinhAnh);
                }
                catch
                {
                    LoadProductImage(null);
                }

            }
            catch
            {
                txtTenMH.Clear();
                txtLoaiMH.Clear();
                txtDVT.Clear();
                txtTrangThai.Clear();
                picMH.Image = null;
            }
        }


        private void FrmLapPN_Load(object sender, EventArgs e)
        {
            SearchCTPhieuNhap();
        }

        private void dgvCTPN_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCTPN.SelectedRows.Count > 0)
            {
                try
                {
                    string MaCT = dgvCTPN.SelectedRows[0].Cells[0].Value.ToString();
                    LoadDetail(new ChiTietPhieuNhapBLL().GetByID(MaCT));
                }
                catch
                {
                    LoadDetail(null);
                }
            }
        }

        private void txtMaPN_TextChanged(object sender, EventArgs e)
        {
            SearchCTPhieuNhap();
        }
    }
}
